package com.bxd.baodan.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.BaoDanAdapter;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.InsuranceItemBean;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.BaoDanDialog;
import com.bxd.baodan.widget.MenuBar;
import com.bxd.baodan.widget.MyListView;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * Created by lizhichao on 17/8/29.
 */

public class BaodanManagerActivity extends BaseActivity implements View.OnClickListener {

    private Button mBtnAnli;
    private LinearLayout mLlBeishu;
    private MyListView mLvBaodan;
    private View lv_ly;
    private BaoDanAdapter mAdapter;

    private Boolean isLogin;
    private List<InsuranceItemBean.ListBean> mData;

    private boolean isLoad = true;
    private ImageView addBaoDan;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baodan_manager_1);
        setActionBarTitle("我的保单");
        initView();
        initData();
    }


    private void initView() {
        lv_ly = findViewById(R.id.lv_layout);
        mBtnAnli = getButton(R.id.btn_anli);
        mLlBeishu = getLinearLayout(R.id.ll_beishu);
        mLvBaodan = findViewById(R.id.lv_baodan);
        addBaoDan = (ImageView) findViewById(R.id.add_baodan_iv);
        addBaoDan.setOnClickListener(this);
        mBtnAnli.setOnClickListener(this);

    }

    private void initData() {
        mAdapter = new BaoDanAdapter(this);
        mLvBaodan.setAdapter(mAdapter);
        mAdapter.setDataIsNullListener(new BaoDanAdapter.DataIsNullListener() {
            @Override
            public void DataIsNull(boolean b) {
                if (b) {
                    mLlBeishu.setVisibility(View.VISIBLE);
                    lv_ly.setVisibility(View.GONE);
                } else {
                    mLlBeishu.setVisibility(View.GONE);
                    lv_ly.setVisibility(View.VISIBLE);
                }
            }
        });

        mLvBaodan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle b = new Bundle();
                b.putString("params", mData.get(position).id);
                MobclickAgent.onEvent(App.getContext(), "v6_1_3_SY_bdgl_ckbdxq");
                openActivity(InsuranceActivity.class, b);
            }
        });

        isLogin = LoginInfo.getInstance(this).isLogin();
        if (isLogin) {
            // 登录了
            loadInfo();
        } else {
            // 没登录
            mLlBeishu.setVisibility(View.VISIBLE);
            lv_ly.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_anli:
                String url = Constant.root_url + "html/insurance/showexample";
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url});
                break;
            case R.id.add_baodan_iv:

                if (loginStatus()) {



                final BaoDanDialog dialog = new BaoDanDialog(BaodanManagerActivity.this).builder();
                dialog.setOnButtonClickListener(new MenuBar.OnButtonClickListener() {
                    @Override
                    public void ButtonClick(int position) {

                        switch (position) {
                            case 0:
                                // 拍照

                                MobclickAgent.onEvent(App.getContext(), "v6_1_3_SY_bdgl_pz");
                                openActivity(TakePicturesAddActivity.class);
                                break;
                            case 1:
                                // 相册
                                MobclickAgent.onEvent(App.getContext(), "v6_1_3_SY_bdgl_xc");
                                openActivity(AlbumAddActivity.class);
                                break;
                            case 2:
                                // 手动
                                MobclickAgent.onEvent(App.getContext(), "v6_1_3_SY_bdgl_sd");
                                openActivity(ManualAdd1Activity.class);
                                break;
                        }
                        dialog.dismiss();

                    }
                });


                dialog.show();

                }



                // TODO: 18/1/27 点击按钮

                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        isLogin = LoginInfo.getInstance(this).isLogin();
        if (isLogin) {
            // 登录了
            loadInfo();
        }
    }

    private void loadInfo() {

        if (isLoad) {
            isLoad = false;

            NetUtil netUtil = new NetUtil(this, JsonApi.INSURANCE_LISTS);
            netUtil.setParams("page", "1");
            netUtil.postRequest("正在加载数据...", new NetUtil.RequestStringListener() {
                @Override
                public void onComplete(String result) {

                    InsuranceItemBean mBean = new Gson().fromJson(JSONUtil.getData(result),
                            new TypeToken<InsuranceItemBean>() {
                            }.getType());
                    mData = mBean.list;

                    if (mData != null && mData.size() > 0) {
                        mLlBeishu.setVisibility(View.GONE);
                        lv_ly.setVisibility(View.VISIBLE);
                    }
                    mAdapter.setData(mData);
                    isLoad = true;
                }
            });
        }
    }


}
